#include <iostream>
using namespace std;
#include<unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
    int pipefd[2] = {0};
    int n = pipe(pipefd);
    if(n<0)
        return 1;
    pid_t id = fork();
    if(id==0)
    {
        //子进程 
        // read
        //::close(pipefd[1]);
        //write
        ::close(pipefd[0]);
        // int count = 0;
        // while(true)
        // {
        //     char buf[1024] = {0};
        //     ::read(pipefd[0],buf,1024);
        //     cout<<buf<<endl;
        //     sleep(2);
        // }
        char buf[1024] = "hello world!";
        while(true)
        {
            ::write(pipefd[1],buf,1024);
            sleep(2);
        }
        exit(0);
    }
    //父进程
    //write
    //::close(pipefd[0]);
    // int count = 0;
    // int total = 0;
    // while(true)
    // {
    //     sleep(5);
    //     string message = "h";
    //     ::write(pipefd[1],message.c_str(),message.size());
    //     total++;
    //     cout<<"total"<<total<<endl;
    //     // string message = "hello world ";
    //     // message += to_string(count++);
    //     // ::write(pipefd[1],message.c_str(),message.size());
    //     //sleep(2);
    // }
    ::close(pipefd[1]);
    //父进程关闭读端口
    ::close(pipefd[0]);
    int status = 0;
    cout<<status<<endl;
    waitpid(id,&status,0);
    cout<<"exit status:"<<(status&0xff)<<endl;
    cout<<"exit code:"<<((status>>8)&0xff)<<endl;
    return 0;
}